﻿using log4net;
using PatientRegisterService.Model;
using PatientRegisterService.Service;
using PatientRegisterService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Services;
using System.Xml;

namespace PatientRegisterService
{
    /// <summary>
    /// PatientRegisterWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class PatientRegisterWebService : WebService
    {

        //1：医生 2：门诊
        private readonly string[] orderTypes = new string[2] { "1", "2" };
        private readonly Dictionary<string, string> channelCodes =
            new Dictionary<string, string>() { { "QYW", "趣医" }, { "HZWM", "微脉" }, { "PAYL", "平安" }, { "DTY", "滇医通" }, { "JKCS", "万达" }, { "BST", "建行" } };

        //趣医      ,建行      ,证通      ,云医微信  ,人社通    ,滇医通    ,微脉      ,平安      ,尚雅科技   ,云医     ,蓝旗      ,万达
        //587512259,618484252,446765459,634692864,757328524,781429863,159223403,387638308,859534506,294073865,843242395，324652659

        private readonly Dictionary<string, string> selfChannelCodes =
            new Dictionary<string, string>() { { "QYW", "587512259" }, { "HZWM", "159223403" }, { "PAYL", "387638308" }, { "DTY", "781429863" }, { "JKCS", "324652659" }, { "BST", "618484252" } };
        private readonly string[] userSexs = new string[2] { "1", "2" };
        private readonly string[] queryPersonTypes = new string[2] { "1", "2" };
        private readonly string[] queryDateTypes = new string[2] { "1", "2" };
        private readonly string[] payType0s = new string[2] { "1", "2" };



        ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        //号源查询
        [WebMethod]
        public string GetOrderNumInfoService(string param)
        {
            log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "接收的参数:" + param);
            string code = "0";
            string desc = "成功";
            XmlDocument doc = new XmlDocument();
            XmlDocument returnXmldoc = new XmlDocument();


            //100：普通门诊 102：专家门诊 999其它 目前支持 
            var orderNumTypes = new string[] { "100", "102", "999", "%" };
            //1：普通查询 2：查询排班日期下有号源的医生|门诊
            var queryTypes = new string[] { "1", "2" };

            byte[] bytes = Encoding.Default.GetBytes(param);
            param = Encoding.UTF8.GetString(bytes);

            try
            {
                #region begin
                doc.LoadXml(param);
                //接入帐号
                var accessAccount = doc.SelectSingleNode("/Request/MessageHeader/accessAccount")?.InnerText?.Trim();
                if (!(accessAccount?.Equals("43120477700")).GetValueOrDefault(false))
                    throw new InvalidOperationException(nameof(accessAccount) + "错误");

                //医院代码
                var hosOrgCode = doc.SelectSingleNode("/Request/NumSourceInfo/hosOrgCode")?.InnerText?.Trim();
                if ((hosOrgCode?.Length ?? 0) != 11)
                    throw new InvalidOperationException(nameof(hosOrgCode) + "错误");
                //一级科室代码
                var oneDeptCode = doc.SelectSingleNode("/Request/NumSourceInfo/oneDeptCode")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(oneDeptCode))
                    throw new InvalidOperationException(nameof(oneDeptCode) + "错误");

                //二级科室代码
                var deptCode = doc.SelectSingleNode("/Request/NumSourceInfo/deptCode")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(deptCode))
                    throw new InvalidOperationException(nameof(deptCode) + "错误");
                //预约类型
                var orderType = doc.SelectSingleNode("/Request/NumSourceInfo/orderType")?.InnerText?.Trim();
                if (!orderTypes.Contains(orderType))
                    throw new InvalidOperationException(nameof(orderType) + "错误2");

                //开始时间
                var startTime = doc.SelectSingleNode("/Request/NumSourceInfo/startTime")?.InnerText?.Trim();
                if (!DateTime.TryParse(startTime, out var dateTemp))
                    throw new InvalidOperationException(nameof(startTime) + "错误");

                //结束时间
                var endTime = doc.SelectSingleNode("/Request/NumSourceInfo/endTime")?.InnerText?.Trim();
                if (!DateTime.TryParse(startTime, out dateTemp))
                    throw new InvalidOperationException(nameof(endTime) + "错误");

                //查询类型
                var queryType = doc.SelectSingleNode("/Request/NumSourceInfo/queryType")?.InnerText?.Trim();
                if (!queryTypes.Contains(queryType))
                    throw new InvalidOperationException(nameof(queryType) + "错误");

                //医生|门诊代码(资源代码，QueryType为2时非必填)
                var resourceCode = doc.SelectSingleNode("/Request/NumSourceInfo/resourceCode")?.InnerText?.Trim();
                if (!queryType.Equals("2") && string.IsNullOrWhiteSpace(resourceCode))
                    throw new InvalidOperationException(nameof(resourceCode) + "错误");
                if (string.IsNullOrWhiteSpace(resourceCode)) resourceCode = "%";

                //号源类型(可为空 即查询所有的类型)
                var orderNumType = doc.SelectSingleNode("/Request/NumSourceInfo/orderNumType")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(orderNumType)) orderNumType = "%";

                if (!orderNumTypes.Contains(orderNumType))
                    throw new InvalidOperationException(nameof(orderNumType) + "错误");

                var yscode = string.Empty;
                if (orderType.Equals("1"))
                {
                    yscode = resourceCode;
                }
                else if (orderType.Equals("2"))
                {
                    yscode = "*";
                }

                XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                XmlElement list = returnXmldoc.CreateElement("List");

                var patientRegistService = new PatientRegistService();
                List<NumSourceInfo> nums =
                    patientRegistService.GetOrderNumInfo(startTime, endTime, yscode, deptCode, orderNumType, queryType.Equals("1") ? false : true);

                foreach (NumSourceInfo num in nums)
                {
                    XMLHelper.XmlData(num, "NumSourceInfo", returnXmldoc, list);
                }
                xmlRoot.AppendChild(list);
                return returnXmldoc.InnerXml;
                #endregion
            }
            catch (Exception e)
            {
                code = "1";
                desc = e.Message;
                if (!code.Equals("0"))
                {
                    XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                }
                return returnXmldoc.InnerXml;
            }
            finally
            {
                log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "返回值:" + returnXmldoc.InnerXml);
            }
        }

        //号源预约
        [WebMethod]
        public string SubmitOrderByUserInfoService(string param)
        {
            log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "接收的参数:" + param);
            string code = "0";
            string desc = "成功";
            string channelName = string.Empty;
            DateTime numSourceDate;
            XmlDocument doc = new XmlDocument();
            XmlDocument returnXmldoc = new XmlDocument();

            try
            {
                doc.LoadXml(param);

                //接入帐号
                var accessAccount = doc.SelectSingleNode("/Request/MessageHeader/accessAccount")?.InnerText?.Trim();
                if (!(accessAccount?.Equals("43120477700")).GetValueOrDefault(false))
                    throw new InvalidOperationException(nameof(accessAccount) + "错误");
                //医疗机构代码
                var hosOrgCode = doc.SelectSingleNode("/Request/OrderInfo/hosOrgCode")?.InnerText?.Trim();
                if ((hosOrgCode?.Length ?? 0) != 11)
                    throw new InvalidOperationException(nameof(hosOrgCode) + "错误");
                //一级科室代码
                var oneDeptCode = doc.SelectSingleNode("/Request/OrderInfo/oneDeptCode")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(oneDeptCode))
                    throw new InvalidOperationException(nameof(oneDeptCode) + "错误");
                //二级科室代码
                var deptCode = doc.SelectSingleNode("/Request/OrderInfo/deptCode")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(deptCode))
                    throw new InvalidOperationException(nameof(deptCode) + "错误");
                //预约类型(1：医生，2：门诊)
                var orderType = doc.SelectSingleNode("/Request/OrderInfo/orderType")?.InnerText?.Trim();
                if (!orderTypes.Contains(orderType))
                    throw new InvalidOperationException(nameof(orderType) + "错误2");
                //医生|门诊代码
                var resourceCode = doc.SelectSingleNode("/Request/OrderInfo/resourceCode")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(resourceCode))
                    throw new InvalidOperationException(nameof(resourceCode) + "错误");
                //预约渠道代码
                var channelCode = doc.SelectSingleNode("/Request/OrderInfo/channelCode")?.InnerText?.Trim();
                if (!channelCodes.Keys.Contains(channelCode))
                    throw new InvalidOperationException(nameof(channelCode) + "错误2");
                channelName = channelCodes[channelCode];
                channelCode = selfChannelCodes[channelCode];
                //排班ID
                var scheduleIdStr = doc.SelectSingleNode("/Request/OrderInfo/scheduleId")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(scheduleIdStr))
                    throw new InvalidOperationException("scheduleId错误");

                var SourcescheduleIdStr = scheduleIdStr.Split('_').First();
                if (!Int32.TryParse(SourcescheduleIdStr, out var scheduleId))
                    throw new InvalidOperationException(nameof(scheduleId) + "错误");
                //号源ID
                //var numSourceId = doc.SelectSingleNode("/Request/OrderInfo/numSourceId")?.InnerText?.Trim();
                //if (!Int32.TryParse(numSourceId, out var numSourceIdTemp))
                //    throw new InvalidOperationException(nameof(numSourceId) + "错误");
                #region 仅为了wonder存
                //支付方式
                var payMode = doc.SelectSingleNode("/Request/OrderInfo/payMode")?.InnerText?.Trim();
                //转诊患者类型
                var patient_type = doc.SelectSingleNode("/Request/OrderInfo/patient_type")?.InnerText?.Trim();
                //1+1+1 签约编号
                var patient_id = doc.SelectSingleNode("/Request/OrderInfo/patient_id")?.InnerText?.Trim();
                #endregion
                //就诊人卡号
                //var mediCardId = doc.SelectSingleNode("/Request/OrderInfo/mediCardId")?.InnerText?.Trim(); 
                //就诊人卡类型
                //var mediCardType = doc.SelectSingleNode("/Request/OrderInfo/mediCardType")?.InnerText?.Trim(); 

                //就诊人证件类型(写死身份证)
                var userCardTypeStr = doc.SelectSingleNode("/Request/OrderInfo/userCardType")?.InnerText?.Trim();
                if (!userCardTypeStr.Equals("1"))
                    throw new InvalidOperationException("userCardType错误");
                var userCardType = 0;
                //就诊人证件号码
                var userCardId = doc.SelectSingleNode("/Request/OrderInfo/userCardId")?.InnerText?.Trim();
                if (!IDCardHelper.CheckIDCard18(userCardId))
                    throw new InvalidOperationException(nameof(userCardId) + "错误1");
                //就诊人姓名
                var userName = doc.SelectSingleNode("/Request/OrderInfo/userName")?.InnerText?.Trim();
                if ((userName?.Length ?? 0) < 2 || (userName?.Length ?? 0) > 4)
                    throw new InvalidOperationException(nameof(userName) + "错误");
                //手机号码
                var userPhone = doc.SelectSingleNode("/Request/OrderInfo/userPhone")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(userPhone))
                    throw new InvalidOperationException(nameof(userPhone) + "错误");
                //就诊人性别
                var userSexStr = doc.SelectSingleNode("/Request/OrderInfo/userSex")?.InnerText?.Trim();
                if (!userSexs.Contains(userSexStr))
                    throw new InvalidOperationException(nameof(userSexStr) + "错误");
                var userSex = Int32.Parse(userSexStr);
                //就诊人生日
                var userBDStr = doc.SelectSingleNode("/Request/OrderInfo/userBD")?.InnerText?.Trim();
                if (!DateTime.TryParse(userBDStr, out var UserBD))
                    throw new InvalidOperationException(nameof(userBDStr) + "错误");
                //就诊人联系地址
                var userContAdd = doc.SelectSingleNode("/Request/OrderInfo/userContAdd")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(userContAdd)) userContAdd = "地址未填写";
                if ((userContAdd?.Length ?? 0) > 50)
                    throw new InvalidOperationException(nameof(userContAdd) + "错误");

                //预约人证件类型
                var replaceUserCardType = doc.SelectSingleNode("/Request/OrderInfo/replaceUserCardType")?.InnerText?.Trim();
                //预约人证件号码
                var replaceUserCardId = doc.SelectSingleNode("/Request/OrderInfo/replaceUserCardId")?.InnerText?.Trim();
                //预约人证件号码
                //var replaceMediCardId = doc.SelectSingleNode("/Request/OrderInfo/replaceMediCardId")?.InnerText?.Trim(); 
                //预约人卡号
                //var replaceMediCardType = doc.SelectSingleNode("/Request/OrderInfo/replaceMediCardType")?.InnerText?.Trim(); 
                //就诊人职业
                var userOccupation = "其他";//doc.SelectSingleNode("/Request/OrderInfo/userOccupation")?.InnerText?.Trim();
                //预约人用户姓名
                var replaceUserName = doc.SelectSingleNode("/Request/OrderInfo/replaceUserName")?.InnerText?.Trim();
                //IP 地址
                var ipAddr = doc.SelectSingleNode("/Request/OrderInfo/ipAddr")?.InnerText?.Trim();
                //Mac 地址
                var macAddr = doc.SelectSingleNode("/Request/OrderInfo/macAddr")?.InnerText?.Trim();
                //预约转诊转出机构代码
                //var outHospitalCode = doc.SelectSingleNode("/Request/OrderInfo/outHospitalCode")?.InnerText?.Trim(); 
                //预约转诊转出机构本地转诊ID
                //var outOrderId = doc.SelectSingleNode("/Request/OrderInfo/outOrderId")?.InnerText?.Trim(); 
                //号源日期
                var numSourceDatStr = doc.SelectSingleNode("/Request/OrderInfo/numSourceDate")?.InnerText?.Trim();
                if (!DateTime.TryParse(numSourceDatStr, out numSourceDate))
                    throw new InvalidOperationException(nameof(numSourceDate) + "错误");
                //就诊_开始时段
                var startTime = doc.SelectSingleNode("/Request/OrderInfo/startTime")?.InnerText?.Trim();
                //就诊_结束时段
                var endTime = doc.SelectSingleNode("/Request/OrderInfo/endTime")?.InnerText?.Trim();
                //民族编码
                //var nationCode = doc.SelectSingleNode("/Request/OrderInfo/nationCode")?.InnerText?.Trim(); 
                //
                var transNo = Guid.NewGuid().ToString();


                var patientRegistService = new PatientRegistService();
                //预约挂号
                var success = patientRegistService.SubmitOrderByUserInfo(channelCode, channelName, numSourceDate, scheduleId, scheduleIdStr, userCardId, userCardType, userCardId, userName, userSex, UserBD, userPhone, userContAdd, transNo, out desc, out var keyNo,
                    payMode, patient_type, patient_id, userCardTypeStr, replaceUserCardType, replaceUserCardId, replaceUserName, ipAddr, macAddr,
                    startTime, endTime, userCardId);
                if (success)
                    code = "0";
                else
                    code = "1";

                XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                XmlElement hosOrgCodeXml = returnXmldoc.CreateElement("hosOrgCode");
                hosOrgCodeXml.InnerText = "43120477700";

                //预约单 ID
                XmlElement orderIdxml = returnXmldoc.CreateElement("orderId");
                orderIdxml.InnerText = keyNo.ToString();
                //号源详情 ID（订单表的id）
                XmlElement numSourceDetailIdxml = returnXmldoc.CreateElement("numSourceDetailId");
                numSourceDetailIdxml.InnerText = keyNo.ToString();

                XmlElement resultXMl = returnXmldoc.CreateElement("Result");
                resultXMl.AppendChild(hosOrgCodeXml);
                resultXMl.AppendChild(orderIdxml);
                resultXMl.AppendChild(numSourceDetailIdxml);

                xmlRoot.AppendChild(resultXMl);

                return returnXmldoc.InnerXml;
            }
            catch (Exception e)
            {
                code = "1";
                desc = e.Message;
                if (!code.Equals("0"))
                {
                    XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                }
                return returnXmldoc.InnerXml;
            }
            finally
            {
                log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "返回值:" + returnXmldoc.InnerXml);
            }
        }

        //预约退号
        [WebMethod]
        public string OrderCancelInfoService(string param)
        {
            log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "接收的参数:" + param);
            string code = "0";
            string desc = "成功";
            XmlDocument doc = new XmlDocument();
            XmlDocument returnXmldoc = new XmlDocument();

            try
            {
                doc.LoadXml(param);

                //接入帐号
                var accessAccount = doc.SelectSingleNode("/Request/MessageHeader/accessAccount")?.InnerText?.Trim();
                if (!(accessAccount?.Equals("43120477700")).GetValueOrDefault(false))
                    throw new InvalidOperationException(nameof(accessAccount) + "错误");
                //医疗机构代码
                var hosOrgCode = doc.SelectSingleNode("/Request/OrderInfo/hosOrgCode")?.InnerText?.Trim();
                if ((hosOrgCode?.Length ?? 0) != 11)
                    throw new InvalidOperationException(nameof(hosOrgCode) + "错误");
                //预约单ID
                var keyNoStr = doc.SelectSingleNode("/Request/OrderInfo/orderId")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(keyNoStr))
                    throw new InvalidOperationException("orderId错误");
                if (!Int32.TryParse(keyNoStr, out var keyNo))
                    throw new InvalidOperationException("orderId错误");

                var delTransNo = Guid.NewGuid().ToString();

                var patientRegistService = new PatientRegistService();
                var success = patientRegistService.OrderCancelInfo(keyNo, delTransNo, out desc);
                if (success)
                    code = "0";
                else
                    code = "1";

                XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                return returnXmldoc.InnerXml;
            }
            catch (Exception e)
            {
                code = "1";
                desc = e.Message;
                if (!code.Equals("0"))
                {
                    XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                }
                return returnXmldoc.InnerXml;
            }
            finally
            {
                log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "返回值:" + returnXmldoc.InnerXml);
            }
        }

        //预约单列表查询
        [WebMethod]
        public string GetOrderListService(string param)
        {
            log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "接收的参数:" + param);
            string code = "0";
            string desc = "成功";
            XmlDocument doc = new XmlDocument();
            XmlDocument returnXmldoc = new XmlDocument();

            try
            {
                doc.LoadXml(param);

                //接入帐号
                var accessAccount = doc.SelectSingleNode("/Request/MessageHeader/accessAccount")?.InnerText?.Trim();
                if (!(accessAccount?.Equals("43120477700")).GetValueOrDefault(false))
                    throw new InvalidOperationException(nameof(accessAccount) + "错误");

                var queryPersonType = doc.SelectSingleNode("/Request/OrderInfo/queryPersonType")?.InnerText?.Trim();
                if (!queryPersonTypes.Contains(queryPersonType))
                    throw new InvalidOperationException(nameof(queryPersonType) + "错误");

                var userCardType = doc.SelectSingleNode("/Request/OrderInfo/userCardType")?.InnerText?.Trim();
                if (!userCardType.Equals("1"))
                    throw new InvalidOperationException("userCardType错误");

                var userCardId = doc.SelectSingleNode("/Request/OrderInfo/userCardId")?.InnerText?.Trim();
                if (!IDCardHelper.CheckIDCard18(userCardId))
                    throw new InvalidOperationException(nameof(userCardId) + "错误1");

                var queryDateType = doc.SelectSingleNode("/Request/OrderInfo/queryDateType")?.InnerText?.Trim();
                if (!queryDateTypes.Contains(queryDateType))
                    throw new InvalidOperationException(nameof(queryDateType) + "错误");

                var startTimeStr = doc.SelectSingleNode("/Request/OrderInfo/startTime")?.InnerText?.Trim();
                if (!DateTime.TryParse(startTimeStr, out var startTime))
                    throw new InvalidOperationException(nameof(startTime) + "错误");
                var endTimeStr = doc.SelectSingleNode("/Request/OrderInfo/endTime")?.InnerText?.Trim();
                if (!DateTime.TryParse(endTimeStr, out var endTime))
                    throw new InvalidOperationException(nameof(endTime) + "错误");

                var patientRegistService = new PatientRegistService();
                List<OrderInfo> nums =
                    patientRegistService.GetOrderList(queryPersonType, userCardType, userCardId, queryDateType, startTime, endTime);

                XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                XmlElement list = returnXmldoc.CreateElement("List");
                foreach (OrderInfo num in nums)
                {
                    XMLHelper.XmlData(num, "OrderInfo", returnXmldoc, list);
                }
                xmlRoot.AppendChild(list);
                return returnXmldoc.InnerXml;
            }
            catch (Exception e)
            {
                code = "1";
                desc = e.Message;
                if (!code.Equals("0"))
                {
                    XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                }
                return returnXmldoc.InnerXml;
            }
            finally
            {
                log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "返回值:" + returnXmldoc.InnerXml);
            }
        }

        //预约单详情查询
        [WebMethod]
        public string GetOrderDetailInfoService(string param)
        {
            log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "接收的参数:" + param);
            string code = "0";
            string desc = "成功";
            XmlDocument doc = new XmlDocument();
            XmlDocument returnXmldoc = new XmlDocument();

            try
            {
                doc.LoadXml(param);

                //接入帐号
                var accessAccount = doc.SelectSingleNode("/Request/MessageHeader/accessAccount")?.InnerText?.Trim();
                if (!(accessAccount?.Equals("43120477700")).GetValueOrDefault(false))
                    throw new InvalidOperationException(nameof(accessAccount) + "错误");

                //医院代码
                var hosOrgCode = doc.SelectSingleNode("/Request/OrderInfo/hosOrgCode")?.InnerText?.Trim();
                if ((hosOrgCode?.Length ?? 0) != 11)
                    throw new InvalidOperationException(nameof(hosOrgCode) + "错误");

                var orderIdStr = doc.SelectSingleNode("/Request/OrderInfo/orderId")?.InnerText?.Trim();
                if (!Int32.TryParse(orderIdStr, out var orderId))
                    throw new InvalidOperationException(nameof(orderId) + "错误");

                var patientRegistService = new PatientRegistService();
                OrderDetailsInfo odi =
                    patientRegistService.GetOrderGetOrderDetailInfo(orderId);

                XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                XMLHelper.XmlData(odi, "OrderInfo", returnXmldoc, xmlRoot);
                return returnXmldoc.InnerXml;
            }
            catch (Exception e)
            {
                code = "1";
                desc = e.Message;
                if (!code.Equals("0"))
                {
                    XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                }
                return returnXmldoc.InnerXml;
            }
            finally
            {
                log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "返回值:" + returnXmldoc.InnerXml);
            }
        }

        //获取可预约医生信息
        [WebMethod]
        public string GetResourceDoctInfoService(string param)
        {
            log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "接收的参数:" + param);
            string code = "0";
            string desc = "成功";
            XmlDocument doc = new XmlDocument();
            XmlDocument returnXmldoc = new XmlDocument();

            try
            {
                doc.LoadXml(param);

                //接入帐号
                var accessAccount = doc.SelectSingleNode("/Request/MessageHeader/accessAccount")?.InnerText?.Trim();
                if (!(accessAccount?.Equals("43120477700")).GetValueOrDefault(false))
                    throw new InvalidOperationException(nameof(accessAccount) + "错误");

                //医院代码
                var hosOrgCode = doc.SelectSingleNode("/Request/DeptInfo/hosOrgCode")?.InnerText?.Trim();
                if ((hosOrgCode?.Length ?? 0) != 11)
                    throw new InvalidOperationException(nameof(hosOrgCode) + "错误");

                //一级科室代码
                //var oneDeptCode = doc.SelectSingleNode("/Request/DeptInfo/oneDeptCode")?.InnerText?.Trim();
                //if (string.IsNullOrWhiteSpace(oneDeptCode))
                //    throw new InvalidOperationException(nameof(oneDeptCode) + "错误");

                //二级科室代码
                var deptCode = doc.SelectSingleNode("/Request/DeptInfo/deptCode")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(deptCode))
                    throw new InvalidOperationException(nameof(deptCode) + "错误");

                var patientRegistService = new PatientRegistService();
                List<ResourceDoct> nums = patientRegistService.GetResourceDoctInfo(deptCode, 0);

                XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                XmlElement list = returnXmldoc.CreateElement("List");
                foreach (ResourceDoct num in nums)
                {
                    XMLHelper.XmlData(num, "ResourceInfo", returnXmldoc, list);
                }
                xmlRoot.AppendChild(list);
                return returnXmldoc.InnerXml;
            }
            catch (Exception e)
            {
                code = "1";
                desc = e.Message;
                if (!code.Equals("0"))
                {
                    XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                }
                return returnXmldoc.InnerXml;
            }
            finally
            {
                log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "返回值:" + returnXmldoc.InnerXml);
            }
        }

        //获取可预约门诊信息
        [WebMethod]
        public string GetResourceOutPatInfoService(string param)
        {
            log.Info("接收的参数:" + param);
            string code = "0";
            string desc = "成功";
            XmlDocument doc = new XmlDocument();
            XmlDocument returnXmldoc = new XmlDocument();

            try
            {
                doc.LoadXml(param);

                //接入帐号
                var accessAccount = doc.SelectSingleNode("/Request/MessageHeader/accessAccount")?.InnerText?.Trim();
                if (!(accessAccount?.Equals("43120477700")).GetValueOrDefault(false))
                    throw new InvalidOperationException(nameof(accessAccount) + "错误");

                //医院代码
                var hosOrgCode = doc.SelectSingleNode("/Request/DeptInfo/hosOrgCode")?.InnerText?.Trim();
                if ((hosOrgCode?.Length ?? 0) != 11)
                    throw new InvalidOperationException(nameof(hosOrgCode) + "错误");

                //一级科室代码
                //var oneDeptCode = doc.SelectSingleNode("/Request/DeptInfo/oneDeptCode")?.InnerText?.Trim();
                //if (string.IsNullOrWhiteSpace(oneDeptCode))
                //    throw new InvalidOperationException(nameof(oneDeptCode) + "错误");

                //二级科室代码
                var deptCode = doc.SelectSingleNode("/Request/DeptInfo/deptCode")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(deptCode))
                    throw new InvalidOperationException(nameof(deptCode) + "错误");

                var patientRegistService = new PatientRegistService();
                List<ResourceDoct> nums = patientRegistService.GetResourceDoctInfo(deptCode, 1);

                XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                XmlElement list = returnXmldoc.CreateElement("List");
                foreach (ResourceDoct num in nums)
                {
                    XMLHelper.XmlData(num, "ResourceInfo", returnXmldoc, list);
                }
                xmlRoot.AppendChild(list);
                return returnXmldoc.InnerXml;
            }
            catch (Exception e)
            {
                code = "1";
                desc = e.Message;
                if (!code.Equals("0"))
                {
                    XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                }
                return returnXmldoc.InnerXml;
            }
            finally
            {
                log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "返回值:" + returnXmldoc.InnerXml);
            }
        }

        //费用变更接口
        [WebMethod]
        public string UpPayStatusService(string param)
        {
            log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "接收的参数:" + param);
            string code = "0";
            string desc = "成功";
            XmlDocument doc = new XmlDocument();
            XmlDocument returnXmldoc = new XmlDocument();

            try
            {
                doc.LoadXml(param);

                //接入帐号
                var accessAccount = doc.SelectSingleNode("/Request/MessageHeader/accessAccount")?.InnerText?.Trim();
                if (!(accessAccount?.Equals("43120477700")).GetValueOrDefault(false))
                    throw new InvalidOperationException(nameof(accessAccount) + "错误");

                //医院代码
                var hosOrgCode = doc.SelectSingleNode("/Request/List/OrderInfo/hosOrgCode")?.InnerText?.Trim();
                if ((hosOrgCode?.Length ?? 0) != 11)
                    throw new InvalidOperationException(nameof(hosOrgCode) + "错误");

                //预约单ID
                var keyNoStr = doc.SelectSingleNode("/Request/List/OrderInfo/orderId")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(keyNoStr))
                    throw new InvalidOperationException("orderId错误");
                if (!Int32.TryParse(keyNoStr, out var keyNo))
                    throw new InvalidOperationException("orderId错误");

                var bankTransNo = doc.SelectSingleNode("/Request/List/OrderInfo/transNo")?.InnerText?.Trim();
                if (string.IsNullOrWhiteSpace(bankTransNo))
                    throw new InvalidOperationException("transNo错误");

                var payMode = doc.SelectSingleNode("/Request/List/OrderInfo/payMode")?.InnerText?.Trim();
                var payAmountStr = doc.SelectSingleNode("/Request/List/OrderInfo/payAmount")?.InnerText?.Trim();
                if (!decimal.TryParse(payAmountStr, out var payAmount))
                    throw new InvalidOperationException("payAmount错误");
                var payType0 = doc.SelectSingleNode("/Request/List/OrderInfo/payType")?.InnerText?.Trim();
                if (!payType0s.Contains(payType0))
                    throw new InvalidOperationException("payType错误");




                var patientRegistService = new PatientRegistService();
                var success = patientRegistService.UpPayStatus(keyNo, bankTransNo, payMode, payAmount, payType0, out desc);
                if (success)
                    code = "0";
                else
                    code = "1";

                XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                return returnXmldoc.InnerXml;
            }
            catch (Exception e)
            {
                code = "1";
                desc = e.Message;
                XmlElement xmlRoot = XMLHelper.XmlHeader(returnXmldoc, code, desc);
                return returnXmldoc.InnerXml;
            }
            finally
            {
                log.Info(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "返回值:" + returnXmldoc.InnerXml);
            }
        }
    }
}
